/*!
 * \file:%{Cpp:License:FileName}
 * \brief:文件说明
 * \version:v1.0
 * \author:郭智隆
 * \date:2017-11-29
 * \b 修改说明
 * \i $修改日期$
 * $修改记录$
 */
#include <QCoreApplication>
#include <iostream>
#include <QThread>
#include <vector>
#include <YSysWatchDog.h>
#include <algorithm>
using namespace std;
const QString g_szInstructionSet[]={"start","get sysDog list","set restart"};
YSysWatchDog * g_pSysDog = 0;
void restart();
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    cout << qApp->applicationName().toLocal8Bit().data() << " start" << endl;
    char c;
    const char * pStr = 0;
    string str;

    QString strCmd;
    while (c = getchar())
    {

        if(c != '\n')
            str+=c;
        else
        {
            strCmd = QString::fromStdString( str);

            if(g_pSysDog == 0)
            {
                cout << "The SysWatchDog is not created and is created [Y/N]" << endl;
                c = getchar();
                if(c == 'Y' || c == 'y')
                {
                    g_pSysDog = new YSysWatchDog;
                    g_pSysDog->init(qApp->applicationDirPath()+"/WatchDog.ini");
                    g_pSysDog->start();
                    g_pSysDog->setRestartFun(&restart);
                }

                continue;

            }
            //str = strComd.to
            if(strCmd.toLower().compare(g_szInstructionSet[0].toLower()) == 0)
            {
                g_pSysDog = new YSysWatchDog;
                g_pSysDog->init(qApp->applicationDirPath()+"/WatchDog.ini");
                g_pSysDog->start();
            }
            else if(strCmd.toLower().compare(g_szInstructionSet[1].toLower()) == 0)
            {
                g_pSysDog->getSessionList();
            }
            else if(strCmd.toLower().compare(g_szInstructionSet[2].toLower()) == 0)
            {
                cout << "Please enter the target address:";
                str.clear();
                while (c = getchar())
                {

                    if(c != '\n')
                        str+=c;
                    else
                    {
                        g_pSysDog->sendRestart(str.data());
                        break;
                    }
                }
            }
            str.clear();
        }
    }
    return a.exec();
}
void restart()
{
    cout << "restart" <<endl;
}
